// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <fmt/format.h>
#include <pollux/core/expressions.h>
#include <pollux/substrait/substrait_parser.h>
#include <pollux/vector/complex_vector.h>

namespace kumo::pollux::substrait {
    /// This class is used to convert Substrait representations to Pollux
    /// expressions.
    class SubstraitPolluxExprConverter {
    public:
        /// subParser: A Substrait parser used to convert Substrait representations
        /// into recognizable representations. functionMap: A pre-constructed map
        /// storing the relations between the function id and the function name.
        explicit SubstraitPolluxExprConverter(
            memory::MemoryPool *pool,
            const melon::F14FastMap<uint64_t, std::string> &functionMap)
            : pool_(pool), functionMap_(functionMap) {
        }

        /// Convert Substrait Field into Pollux Field Expression.
        std::shared_ptr<const core::FieldAccessTypedExpr> toPolluxExpr(
            const ::substrait::Expression::FieldReference &substraitField,
            const RowTypePtr &inputType);

        /// Convert Substrait ScalarFunction into Pollux Expression.
        core::TypedExprPtr toPolluxExpr(
            const ::substrait::Expression::ScalarFunction &substraitFunc,
            const RowTypePtr &inputType);

        /// Convert Substrait CastExpression to Pollux Expression.
        core::TypedExprPtr toPolluxExpr(
            const ::substrait::Expression::Cast &castExpr,
            const RowTypePtr &inputType);

        /// Convert Substrait Literal into Pollux Expression.
        std::shared_ptr<const core::ConstantTypedExpr> toPolluxExpr(
            const ::substrait::Expression::Literal &substraitLit);

        /// Convert Substrait Expression into Pollux Expression.
        core::TypedExprPtr toPolluxExpr(
            const ::substrait::Expression &substraitExpr,
            const RowTypePtr &inputType);

        /// Convert Substrait IfThen into Pollux Expression.
        core::TypedExprPtr toPolluxExpr(
            const ::substrait::Expression::IfThen &substraitIfThen,
            const RowTypePtr &inputType);

    private:
        /// Convert list literal to ArrayVector.
        ArrayVectorPtr literalsToArrayVector(
            const ::substrait::Expression::Literal &listLiteral);

        /// Memory pool.
        memory::MemoryPool *pool_;

        /// The Substrait parser used to convert Substrait representations into
        /// recognizable representations.
        SubstraitParser substraitParser_;

        /// The map storing the relations between the function id and the function
        /// name.
        melon::F14FastMap<uint64_t, std::string> functionMap_;
    };
} // namespace kumo::pollux::substrait

template<>
struct fmt::formatter<substrait::Expression::RexTypeCase> : formatter<int> {
    auto format(const substrait::Expression::RexTypeCase &s, format_context &ctx)
    const {
        return formatter<int>::format(static_cast<int>(s), ctx);
    }
};

template<>
struct fmt::formatter<substrait::Expression::Cast::FailureBehavior>
        : formatter<int> {
    auto format(
        const substrait::Expression::Cast::FailureBehavior &s,
        format_context &ctx) const {
        return formatter<int>::format(static_cast<int>(s), ctx);
    }
};

template<>
struct fmt::formatter<substrait::Expression_FieldReference::ReferenceTypeCase>
        : formatter<int> {
    auto format(
        const substrait::Expression_FieldReference::ReferenceTypeCase &s,
        format_context &ctx) const {
        return formatter<int>::format(static_cast<int>(s), ctx);
    }
};

template<>
struct fmt::formatter<substrait::Expression_Literal::LiteralTypeCase>
        : formatter<int> {
    auto format(
        const substrait::Expression_Literal::LiteralTypeCase &s,
        format_context &ctx) const {
        return formatter<int>::format(static_cast<int>(s), ctx);
    }
};
